﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using SpaceSim;

namespace SpaceEditorConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Universe uni = new Universe("Resources/glossary.xml");
            SolarSystem solar = null;

            while (true)
            {
                Console.WriteLine("(C)reate system. (S)ave system.  (L)oad system.  E(x)it:");
                Console.Write(">> ");
                ConsoleKey key = Console.ReadKey().Key;
                Console.WriteLine();
                Console.WriteLine();

                switch (key)
                {
                    // Create a new solar system
                    case ConsoleKey.C:
                        solar = uni.BuildSolarSystem();
                        goto case ConsoleKey.P;

                    // Print out solar system
                    case ConsoleKey.D:
                    case ConsoleKey.P:
                        {
                            if (solar == null)
                            {
                                Console.WriteLine("    Must create solar system first");
                                break;
                            }

                            Console.WriteLine("    " + solar.Name);
                            Console.WriteLine("    : {0} [{1}: Mass {2:f4}, Radius {3:f4}]", solar.Primary.Name, solar.Primary.Desc.Name, solar.Primary.Desc.Mass, solar.Primary.Desc.Radius);
                            foreach (AstroBody ab in solar.AstroBodies)
                            {
                                Console.WriteLine("    : {0} [{1}: Mass {2:f4}, Radius {3:f4}]", ab.Name, ab.Desc.Name, ab.Desc.Mass, ab.Desc.Radius);
                            }
                        }
                        break;

                    // Print out solar system, verbosly
                    case ConsoleKey.V:
                        {
                            if (solar == null)
                            {
                                Console.WriteLine("    Must create solar system first");
                                break;
                            }

                            Console.WriteLine("    " + solar.Name);
                            Console.WriteLine("    : {0} [{1}: Mass {2:f4}, Radius {3:f4}, Prob. {4:f4}]", solar.Primary.Name, solar.Primary.Desc.Name, solar.Primary.Desc.Mass, solar.Primary.Desc.Radius, solar.Primary.Desc.Probability);
                            foreach (AstroBody ab in solar.AstroBodies)
                            {
                                Console.WriteLine("    : {0} [{1}: Mass {2:f4}, Radius {3:f4}, Prob. {4:f4}]", ab.Name, ab.Desc.Name, ab.Desc.Mass, ab.Desc.Radius, ab.Desc.Probability);
                            }
                        }
                        break;

                    // Exit program
                    case ConsoleKey.Escape:
                    case ConsoleKey.X:
                        return;

                    // Save current solar system to file
                    case ConsoleKey.S:
                        {
                            if (solar == null)
                            {
                                Console.WriteLine("    Must create solar system first");
                                break;
                            }

                            Console.WriteLine("    Enter a file name:");
                            Console.Write("    >> ");
                            string fileName = Console.ReadLine();
                            if (!fileName.EndsWith(SolarSystem.PreferredFileExtension))
                            {
                                fileName += SolarSystem.PreferredFileExtension;
                            }

                            Console.WriteLine("    Saving to \"" + fileName + "\"");
                            uni.SaveSolarSystem(solar, fileName);
                        }
                        break;

                    // Load solar system from file
                    case ConsoleKey.L:
                        {
                            Console.WriteLine("    Enter a file name:");
                            Console.Write("    >> ");
                            string fileName = Console.ReadLine();
                            if (!fileName.EndsWith(SolarSystem.PreferredFileExtension))
                            {
                                fileName += SolarSystem.PreferredFileExtension;
                            }

                            Console.WriteLine("    Loading from \"" + fileName + "\"");
                            Console.WriteLine();
                            solar = uni.LoadSolarSystem(fileName);
                        }
                        goto case ConsoleKey.P;

                    // Print help
                    case ConsoleKey.H:
                        {
                            Console.WriteLine("    Program Help");
                            Console.WriteLine("        : Press [H] key for this message");
                            Console.WriteLine("        : Press [X] key to exit this program");
                            Console.WriteLine("        : Press [S] key to save current solar system to file");
                            Console.WriteLine("        : Press [L] key to load a solar system from file");
                            Console.WriteLine("        : Press [C] key to create a new solar system");
                            Console.WriteLine("        : Press [P] key to print out the current solar system");
                            Console.WriteLine("        : Press [V] key to print out the current solar system (extra verbose)");
                        }
                        break;

                    default:
                        Console.WriteLine("    No command mapped to \"" + key.ToString() + "\"");
                        goto case ConsoleKey.H;
                }

                Console.WriteLine();
                Console.WriteLine();              
            }
        }
    }
}
